package com.sxhuayuan.parking.controller.agent;

import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.Agent;
import com.sxhuayuan.parking.entity.OemConfig;
import com.sxhuayuan.parking.service.AgentService;
import com.sxhuayuan.parking.service.OemConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * oem设置
 * 
 * @author helong
 * @create 2019-08-15 11:29
 */
@RestController("agentOemConfigController")
@RequestMapping("/agent/oemconfig")
public class OemConfigController extends BaseController {
	@Resource(name = "oemConfigServiceImpl")
	OemConfigService service;
	@Resource(name = "agentServiceImpl")
	AgentService agentService;

	@PostMapping("/update")
	public RespMessage update(@Validated @RequestBody OemConfig config) {
		Agent current = agentService.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		OemConfig oemConfig = current.getOemConfig();
		if (oemConfig == null) {
			return RespMessage.error("未配置oem");
		}
		if (!oemConfig.getOwnerId().equals(current.getId())) {
			return RespMessage.error("无权修改");
		}
		config.setId(oemConfig.getId());
		config.setOwnerId(oemConfig.getOwnerId());
		service.update(config);
		return RespMessage.success();
	}

	@GetMapping("/get")
	public RespMessage get() {
		Agent current = agentService.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		return successResp(current.getOemConfig());
	}
}
